From: Yehuda Katz Date: Thu, 19 Jun 2014 17:12:51 +0000 (-0700) Subject: Require human-tagging errors X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~999 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=e4f4f471926aac44a88396aae4b7354be573e9ef;p=cargo.git Require human-tagging errors --- diff --git a/src/bin/cargo-git-checkout.rs b/src/bin/cargo-git-checkout.rs index 6f3f37425..326d8d6a5 100644 --- a/src/bin/cargo-git-checkout.rs +++ b/src/bin/cargo-git-checkout.rs @@ -9,7 +9,7 @@ use hammer::FlagConfig; use cargo::{execute_main_without_stdin}; use cargo::core::source::{Source,SourceId}; use cargo::sources::git::{GitSource}; -use cargo::util::{Config, CliResult, CliError, Require}; +use cargo::util::{Config, CliResult, CliError, Require, human}; use url::Url; #[deriving(PartialEq,Clone,Decodable)] @@ -29,7 +29,7 @@ fn execute(options: Options) -> CliResult> { let Options { url, reference, .. } = options; let url: Url = try!(from_str(url.as_slice()) - .require(|| format!("The URL `{}` you passed was not a valid URL", url)) + .require(|| human(format!("The URL `{}` you passed was not a valid URL", url))) .map_err(|e| CliError::from_boxed(e, 1))); let source_id = SourceId::for_git(&url, reference.as_slice()); diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 15c2762a6..75608afbe 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -13,7 +13,7 @@ use std::io::process::{Command,InheritFd,ExitStatus,ExitSignal}; use serialize::Encodable; use cargo::{NoFlags,execute_main_without_stdin,handle_error}; use cargo::util::important_paths::find_project; -use cargo::util::{CliError, CliResult, Require, config}; +use cargo::util::{CliError, CliResult, Require, config, human}; fn main() { execute(); @@ -67,7 +67,7 @@ fn execute() { fn process(args: Vec) -> CliResult<(String, Vec)> { let args: Vec = Vec::from_slice(args.tail()); - let head = try!(args.iter().nth(0).require(|| "No subcommand found").map_err(|err| CliError::from_boxed(err, 1))).to_str(); + let head = try!(args.iter().nth(0).require(|| human("No subcommand found")).map_err(|err| CliError::from_boxed(err, 1))).to_str(); let tail = Vec::from_slice(args.tail()); Ok((head, tail)) @@ -133,7 +133,7 @@ fn locate_project(_: NoFlags) -> CliResult> { let root = try!(find_project(os::getcwd(), "Cargo.toml").map_err(|e| CliError::from_boxed(e, 1))); let string = try!(root.as_str() - .require(|| "Your project path contains characters not representable in Unicode") + .require(|| human("Your project path contains characters not representable in Unicode")) .map_err(|e| CliError::from_boxed(e, 1))); Ok(Some(ProjectLocation { root: string.to_str() })) diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 0830956b0..61ce7ad09 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -20,7 +20,7 @@ extern crate hamcrest; use serialize::{Decoder,Encoder,Decodable,Encodable,json}; use std::io; use hammer::{FlagDecoder,FlagConfig,HammerError}; -pub use util::{CliError, CliResult}; +pub use util::{CliError, CliResult, human}; macro_rules! some( ($e:expr) => ( diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 1ca4e2eee..20544a0bf 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -20,7 +20,7 @@ use core::{Source,SourceId,PackageSet,resolver}; use core::registry::PackageRegistry; use ops; use sources::{PathSource}; -use util::{CargoResult, Wrap, config, error}; +use util::{CargoResult, Wrap, config, error, human}; pub fn compile(manifest_path: &Path) -> CargoResult<()> { log!(4, "compile; manifest-path={}", manifest_path.display()); @@ -37,9 +37,9 @@ pub fn compile(manifest_path: &Path) -> CargoResult<()> { let source_ids = package.get_source_ids(); let mut registry = try!(PackageRegistry::new(source_ids, override_ids)); - let resolved = try!(resolver::resolve(package.get_dependencies(), &mut registry).wrap("unable to resolve dependencies")); + let resolved = try!(resolver::resolve(package.get_dependencies(), &mut registry).wrap(human("unable to resolve dependencies"))); - let packages = try!(registry.get(resolved.as_slice()).wrap("unable to get packages from source")); + let packages = try!(registry.get(resolved.as_slice()).wrap(human("unable to get packages from source"))); debug!("packages={}", packages); diff --git a/src/cargo/ops/cargo_read_manifest.rs b/src/cargo/ops/cargo_read_manifest.rs index 258b1cd11..a23e6e023 100644 --- a/src/cargo/ops/cargo_read_manifest.rs +++ b/src/cargo/ops/cargo_read_manifest.rs @@ -4,7 +4,7 @@ use core::{Package,Manifest,SourceId}; use util::{CargoResult, human}; pub fn read_manifest(contents: &[u8], source_id: &SourceId) -> CargoResult<(Manifest, Vec)> { - util::toml::to_manifest(contents, source_id).map_err(human) + util::toml::to_manifest(contents, source_id).map_err(|err| human(err.to_str())) } pub fn read_package(path: &Path, source_id: &SourceId) -> CargoResult<(Package, Vec)> { diff --git a/src/cargo/ops/cargo_rustc.rs b/src/cargo/ops/cargo_rustc.rs index eb5fc6d68..59b7f42f0 100644 --- a/src/cargo/ops/cargo_rustc.rs +++ b/src/cargo/ops/cargo_rustc.rs @@ -3,7 +3,7 @@ use std::io; use std::path::Path; use core::{Package,PackageSet,Target}; use util; -use util::{CargoResult, CargoError, ProcessBuilder, error, human}; +use util::{CargoResult, ProcessBuilder, error, human}; type Args = Vec; @@ -60,15 +60,9 @@ fn rustc(root: &Path, target: &Target, dest: &Path, deps: &Path, verbose: bool) let rustc = prepare_rustc(root, target, *crate_type, dest, deps); try!(if verbose { - rustc.exec().map_err(|err| { - log!(5, "exec failed; error={}", err.description()); - human(err) - }) + rustc.exec().map_err(|err| human(err.to_str())) } else { - rustc.exec_with_output().and(Ok(())).map_err(|err| { - log!(5, "exec_with_output failed; error={}", err.description()); - human(err) - }) + rustc.exec_with_output().and(Ok(())).map_err(|err| human(err.to_str())) }); } diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index d3e3ae852..6c6b457b6 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -1,5 +1,5 @@ use url::Url; -use util::{CargoResult, ChainError, ProcessBuilder, process}; +use util::{CargoResult, ChainError, ProcessBuilder, process, human}; use std::fmt; use std::fmt::{Show,Formatter}; use std::str; @@ -227,11 +227,11 @@ impl GitCheckout { let dirname = Path::new(self.location.dirname()); cargo_try!(mkdir_recursive(&dirname, UserDir).chain_error(|| - format!("Couldn't mkdir {}", Path::new(self.location.dirname()).display()))); + human(format!("Couldn't mkdir {}", Path::new(self.location.dirname()).display())))); if self.location.exists() { cargo_try!(rmdir_recursive(&self.location).chain_error(|| - format!("Couldn't rmdir {}", Path::new(&self.location).display()))); + human(format!("Couldn't rmdir {}", Path::new(&self.location).display())))); } git!(dirname, "clone --no-checkout --quiet {} {}", self.get_source().display(), self.location.display()); @@ -262,12 +262,12 @@ fn git(path: &Path, str: &str) -> ProcessBuilder { } fn git_inherit(path: &Path, str: String) -> CargoResult<()> { - git(path, str.as_slice()).exec().chain_error(|| format!("Executing `git {}` failed", str)) + git(path, str.as_slice()).exec().chain_error(|| human(format!("Executing `git {}` failed", str))) } fn git_output(path: &Path, str: String) -> CargoResult { let output = cargo_try!(git(path, str.as_slice()).exec_with_output().chain_error(|| - format!("Executing `git {}` failed", str))); + human(format!("Executing `git {}` failed", str)))); Ok(to_str(output.output.as_slice()).as_slice().trim_right().to_str()) } diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index aee0bb83c..75609be15 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -2,7 +2,7 @@ use std::{io,fmt,os}; use std::collections::HashMap; use serialize::{Encodable,Encoder}; use toml; -use util::{CargoResult, ChainError, Require, error, internal_error}; +use util::{CargoResult, ChainError, Require, error, internal_error, human}; pub struct Config { home_path: Path @@ -12,7 +12,7 @@ impl Config { pub fn new() -> CargoResult { Ok(Config { home_path: cargo_try!(os::homedir() - .require(|| "Couldn't find the home directory")) + .require(|| human("Couldn't find the home directory"))) }) } diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index c23fddb72..adad8a8db 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -104,16 +104,6 @@ impl ChainError for Result { } } -impl CargoError for &'static str { - fn description(&self) -> String { self.to_str() } - fn is_human(&self) -> bool { true } -} - -impl CargoError for String { - fn description(&self) -> String { self.to_str() } - fn is_human(&self) -> bool { true } -} - impl CargoError for IoError { fn description(&self) -> String { self.to_str() } } @@ -143,7 +133,7 @@ impl Show for ProcessError { Some(ExitStatus(i)) | Some(ExitSignal(i)) => i.to_str(), None => "never executed".to_str() }; - write!(f, "process failed: `{}` (status={})", self.command, exit) + write!(f, "{} (status={})", self.msg, exit) } } @@ -205,7 +195,12 @@ pub struct CliError { } impl CliError { - pub fn new(error: E, code: uint) -> CliError { + pub fn new(error: S, code: uint) -> CliError { + let error = human(error.as_slice().to_str()); + CliError::from_boxed(error, code) + } + + pub fn from_error(error: E, code: uint) -> CliError { let error = box error as Box; CliError::from_boxed(error, code) } @@ -214,7 +209,7 @@ impl CliError { let error = if error.is_human() { error } else { - chain(error, "An unknown error occurred") + chain(error, human("An unknown error occurred")) }; CliError { error: error, exit_code: code } @@ -250,10 +245,13 @@ pub fn error(error: S1) -> Box { } as Box } -pub fn human(error: E) -> Box { - let mut concrete = error.concrete(); - concrete.is_human = true; - box concrete as Box +pub fn human(error: S) -> Box { + box ConcreteCargoError { + description: error.as_slice().to_str(), + detail: None, + cause: None, + is_human: true + } as Box } pub fn chain(original: Box, update: E) -> Box { diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 22898713f..413ba1a7c 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -7,7 +7,7 @@ use serialize::Decodable; use core::{SourceId,GitKind}; use core::manifest::{LibKind,Lib}; use core::{Summary,Manifest,Target,Dependency,PackageId}; -use util::{CargoResult, Require, error}; +use util::{CargoResult, Require, error, human}; pub fn to_manifest(contents: &[u8], source_id: &SourceId) -> CargoResult<(Manifest, Vec)> { let root = try!(toml::parse_from_bytes(contents).map_err(|_| error("Cargo.toml is not valid Toml"))); @@ -33,7 +33,7 @@ fn toml_to_manifest(root: toml::Value) -> CargoResult { let deps = match deps { Some(deps) => { - let table = try!(deps.get_table().require(|| "dependencies must be a table")).clone(); + let table = try!(deps.get_table().require(|| human("dependencies must be a table"))).clone(); let mut deps: HashMap = HashMap::new(); @@ -45,13 +45,13 @@ fn toml_to_manifest(root: toml::Value) -> CargoResult { for (k, v) in table.iter() { let v = try!(v.get_str() - .require(|| "dependency values must be string")); + .require(|| human("dependency values must be string"))); details.insert(k.clone(), v.clone()); } let version = try!(details.find_equiv(&"version") - .require(|| "dependencies must include a version")).clone(); + .require(|| human("dependencies must include a version"))).clone(); deps.insert(k.clone(), DetailedDep(DetailedTomlDependency { version: version,